<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="80%" :close-on-click-modal="false"> 
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="140px" v-loading="formLoading">


      <el-row :gutter="24">
        <el-tag type="info" style="width: 100%;height: 30px;margin-bottom: 20px;">
        <h3>企业基本信息</h3>
      </el-tag>
        <el-col :span="8">
          <el-form-item label="企业名称" prop="companyName">
            <el-input v-model="formData.companyName" placeholder="请输入企业名称" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="统一社会信用代码" prop="unifiedSocialCreditCode">
            <el-input v-model="formData.unifiedSocialCreditCode" placeholder="请输入统一社会信用代码" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="成立日期" prop="establishmentDate">
            <el-date-picker
v-model="formData.establishmentDate" type="date" value-format="YYYY-MM-DD"
              placeholder="选择成立日期" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="注册资金" prop="registeredCapital">
            <el-input-number
v-model="formData.registeredCapital" :min="0" :precision="2" :step="1000"
              controls-position="right" placeholder="请输入注册资金，单位：万元" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="公司类型" prop="companyType">
            <el-input v-model="formData.companyType" placeholder="请输入公司类型" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属经济城" prop="economicZone">
            <el-input v-model="formData.economicZone" placeholder="请输入所属经济城" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="注册地址" prop="registeredAddress">
            <el-input v-model="formData.registeredAddress" placeholder="请输入注册地址" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="经营范围" prop="businessScope">
            <el-input v-model="formData.businessScope" placeholder="请输入经营范围" type="textarea" :rows="3" />
          </el-form-item>
        </el-col>
        
        
        <el-tag type="info" style="width: 100%;height: 30px;margin-bottom: 20px;">
        <h3>企业相关人员信息</h3>
      </el-tag>
        <el-col :span="8">
          <el-form-item label="法人姓名" prop="legalRepresentative">
            <el-input v-model="formData.legalRepresentative" placeholder="请输入法人姓名" />
          </el-form-item>
        </el-col>
         <el-col :span="8">
          <el-form-item label="法人电话号码" prop="legalPersonPhone">
            <el-input v-model="formData.legalPersonPhone" placeholder="请输入法人电话号码" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="监事" prop="supervisor">
            <el-input v-model="formData.supervisor" placeholder="请输入监事" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="企业联系人" prop="contactName">
            <el-input v-model="formData.contactName" placeholder="请输入企业联系人" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系人电话" prop="contactPhone">
            <el-input v-model="formData.contactPhone" placeholder="请输入联系人电话" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招商负责人" prop="investmentManager">
            <el-input v-model="formData.investmentManager" placeholder="请输入招商负责人" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="介绍人" prop="referrer">
            <el-input v-model="formData.referrer" placeholder="请输入介绍人" />
          </el-form-item>
        </el-col>

        
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="formData.remarks" placeholder="请输入备注" type="textarea" :rows="3" />
          </el-form-item>
        </el-col>
      </el-row>


    </el-form>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { RegisteredCompanyApi, RegisteredCompanyVO } from '@/api/oa/registeredcompany'
import type { FormInstance, FormRules } from 'element-plus'

/** 注册企业信息 表单 */
defineOptions({ name: 'RegisteredCompanyForm' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const dialogLoading = ref(false) // 表单的加载中：提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改；detail - 详情
const formData = ref<RegisteredCompanyVO>({
  id: undefined,
  companyName: undefined,
  unifiedSocialCreditCode: undefined,
  economicZone: undefined,
  registeredCapital: undefined,
  registeredAddress: undefined,
  establishmentDate: undefined,
  legalRepresentative: undefined,
  supervisor: undefined,
  contactName: undefined,
  contactPhone: undefined,
  investmentManager: undefined,
  referrer: undefined,
  businessScope: undefined,
  remarks: undefined,
  creatorName: undefined,
  updaterName: undefined,
  createTime: undefined,
  updateTime: undefined,
  creator: undefined,
  updater: undefined,
  deleted: false,
  companyType: undefined // 公司类型
})
const formRef = ref<FormInstance>() // 表单 Ref
const rules = reactive<FormRules>({
  companyName: [{ required: true, message: '企业名称不能为空', trigger: 'blur' }],
  unifiedSocialCreditCode: [{ required: true, message: '统一社会信用代码不能为空', trigger: 'blur' }],
  economicZone: [{ required: true, message: '所属经济城不能为空', trigger: 'blur' }]
})

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await RegisteredCompanyApi.getRegisteredCompany(id)
    } finally {
      formLoading.value = false
    }
  }
  // 详情模式下，禁用表单
  if (type === 'detail') {
    formRef.value?.disable()
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as RegisteredCompanyVO
    if (formType.value === 'create') {
      await RegisteredCompanyApi.createRegisteredCompany(data)
      message.success(t('common.createSuccess'))
    } else {
      await RegisteredCompanyApi.updateRegisteredCompany(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    companyName: undefined,
    unifiedSocialCreditCode: undefined,
    economicZone: undefined,
    registeredCapital: undefined,
    registeredAddress: undefined,
    establishmentDate: undefined,
    legalRepresentative: undefined,
    supervisor: undefined,
    contactName: undefined,
    contactPhone: undefined,
    investmentManager: undefined,
    referrer: undefined,
    businessScope: undefined,
    remarks: undefined,
    creatorName: undefined,
    updaterName: undefined,
    createTime: undefined,
    updateTime: undefined,
    creator: undefined,
    updater: undefined,
    deleted: false
  }
  formRef.value?.resetFields()
}
</script>